package com.cjjd.c.feature.home.chekin.self_select;

import android.support.constraint.ConstraintLayout;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.SelectHomeItemBean;
import com.cjjd.c.view.CountDownTextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/2.
 */

public class SelfSelectHomeAdapter extends MyBaseAdapter {
    public SelfSelectHomeAdapter(FragmentActivity mContext) {
        super(mContext);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_self_select_home, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        SelectHomeItemBean itemBean = (SelectHomeItemBean) getItem(position);
        if ("1".equals(itemBean.isChooseRoom)) {
            viewHolder.clItem.setBackgroundColor(mContext.getResources().getColor(R.color.purple));
            viewHolder.tvName.setVisibility(View.VISIBLE);
            viewHolder.tvRoomNum.setVisibility(View.VISIBLE);
            viewHolder.tvHomeStatus.setVisibility(View.VISIBLE);


            viewHolder.tvName.setText(itemBean.chekInName);
            viewHolder.tvRoomNum.setText(itemBean.roomNo);
            viewHolder.tvHomeStatus.setText(itemBean.unlockedTime);

            final ViewHolder finalViewHolder = viewHolder;
            viewHolder.tvHomeStatus.setOnCountDownFinishedListener(new CountDownTextView.OnCountDownFinishedListener() {
                @Override
                public void onFinish() {
                    // TODO: 2017/12/4 刷新状态
                    EventBus.getDefault().post(EventBusKeys.refreshSelfSelect);
                }
            });

        } else if ("0".equals(itemBean.isChooseRoom)) {
            viewHolder.clItem.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.bg_item_self_select));
            viewHolder.tvName.setVisibility(View.INVISIBLE);
            viewHolder.tvRoomNum.setVisibility(View.INVISIBLE);
            viewHolder.tvHomeStatus.setVisibility(View.INVISIBLE);

        }


        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_room_num)
        TextView tvRoomNum;
        @Bind(R.id.tv_home_status)
        CountDownTextView tvHomeStatus;
        @Bind(R.id.cl_item)
        ConstraintLayout clItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
